package cn.i96688.scanclient.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.math.BigDecimal;

import javax.swing.JLabel;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

@SuppressWarnings("serial")
public class StatusLine extends SuperPane implements TableModelListener{
	JLabel labTxt=new JLabel();
	OrderMsg orderMsg=new OrderMsg();
	public StatusLine(){
		setLayout(new BorderLayout());
		labTxt.setHorizontalAlignment(JLabel.RIGHT);
		labTxt.setForeground(Color.BLUE);
		add(labTxt,BorderLayout.CENTER);
		labTxt.setFont(new Font("宋体",Font.PLAIN,12));
	}
	public void setMessage(String msg){
		labTxt.setText(msg);
		if(msg==null || msg.trim().length()==0){
			orderMsg.clear();
		}
	}
	@Override
	public void tableChanged(TableModelEvent e) {
		/*
		if(e.getType()==TableModelEvent.INSERT){
			DefaultTableModel model=(DefaultTableModel)e.getSource();
			int row=e.getFirstRow();
			String status=(String)model.getValueAt(row, 5);
			Object payAmtObj=model.getValueAt(row, 2);
			BigDecimal payAmt=payAmtObj==null || "".equals(payAmtObj)?BigDecimal.ZERO:new BigDecimal(payAmtObj.toString());
			orderMsg.add(status,payAmt);
			labTxt.setText(orderMsg.toString());
		}*/
		DefaultTableModel model=(DefaultTableModel)e.getSource();
		orderMsg.clear();
		int rows=model.getRowCount();
		for(int i=0;i<rows;i++){
			String status=(String)model.getValueAt(i, CurOrderListPane.INDEX_STATUS);
			Object payAmtObj=model.getValueAt(i, CurOrderListPane.INDEX_PAYABLE);
			BigDecimal payAmt=payAmtObj==null || "".equals(payAmtObj)?BigDecimal.ZERO:new BigDecimal(payAmtObj.toString());
			orderMsg.add(status,payAmt);
		}
		labTxt.setText(orderMsg.toString());
	}
	
	class OrderMsg{
		int total=0;
		int sucNum=0;
		BigDecimal totalAmt=BigDecimal.ZERO;
		BigDecimal sucAmt=BigDecimal.ZERO;
		
		void clear(){
			total=0;
			sucNum=0;
			totalAmt=BigDecimal.ZERO;
			sucAmt=BigDecimal.ZERO;
		}
		void add(String status,BigDecimal amt){
			total++;
			totalAmt=totalAmt.add(amt).setScale(2,BigDecimal.ROUND_HALF_UP);
			//支付成功
			if("1".equals(status)){
				sucNum++;
				sucAmt=sucAmt.add(amt).setScale(2,BigDecimal.ROUND_HALF_UP);
			}
		}
		public String toString(){
			StringBuilder sb=new StringBuilder(" ");
			if(total>0){
				sb.append("总笔数：").append(total).append("，总金额：").append(totalAmt.toString());
			}
			if(sucNum>0){
				sb.append("；  成交笔数：").append(sucNum).append("，成交金额：").append(sucAmt.toString());
			}
			sb.append("  ");
			return sb.toString();
		}
	}
}
